package com.zhouxiaoxuan13.promotionservice.controller;

import com.zhouxiaoxuan13.promotionservice.entity.Promotion;
import com.zhouxiaoxuan13.promotionservice.dto.PromotionDetailDTO;
import com.zhouxiaoxuan13.promotionservice.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/promotions") // Base path for all promotion-related APIs
@CrossOrigin(originPatterns = "*")
public class PromotionController {

    private final PromotionService promotionService;

    @Autowired
    public PromotionController(PromotionService promotionService) {
        this.promotionService = promotionService;
    }

    // GET: Retrieve all promotions with product details
    @GetMapping
    public ResponseEntity<List<PromotionDetailDTO>> getAllPromotions() {
        List<PromotionDetailDTO> promotions = promotionService.getAllPromotionsWithDetails();
        return ResponseEntity.ok(promotions);
    }

    // GET: Retrieve all currently active promotions with product details
    @GetMapping("/active")
    public ResponseEntity<List<PromotionDetailDTO>> getActivePromotions() {
        List<PromotionDetailDTO> activePromotions = promotionService.getActivePromotionsWithDetails();
        return ResponseEntity.ok(activePromotions);
    }

    // GET: Retrieve basic promotions (without product details) - for backward compatibility
    @GetMapping("/basic")
    public ResponseEntity<List<Promotion>> getBasicPromotions() {
        List<Promotion> promotions = promotionService.getAllPromotions();
        return ResponseEntity.ok(promotions);
    }

    // POST: Sync promotion prices with fresh products
    @PostMapping("/actions/sync-prices")
    public ResponseEntity<String> syncPromotionPrices() {
        try {
            promotionService.syncPromotionPrices();
            return ResponseEntity.ok("促销价格同步成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("促销价格同步失败: " + e.getMessage());
        }
    }

    // GET: Retrieve a single promotion by ID
    @GetMapping("/{id}")
    public ResponseEntity<Promotion> getPromotionById(@PathVariable("id") Long id) {
        return promotionService.getPromotionById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // POST: Create a new promotion
    @PostMapping
    public ResponseEntity<Promotion> createPromotion(@RequestBody Promotion promotion) {
        try {
            Promotion createdPromotion = promotionService.createPromotion(promotion);
            return new ResponseEntity<>(createdPromotion, HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(null);
        }
    }

    // PUT: Update an existing promotion by ID
    @PutMapping("/{id}")
    public ResponseEntity<Promotion> updatePromotion(@PathVariable("id") Long id, @RequestBody Promotion promotionDetails) {
        try {
            return promotionService.updatePromotion(id, promotionDetails)
                    .map(ResponseEntity::ok)
                    .orElse(ResponseEntity.notFound().build());
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(null); // Or a more specific error response
        }
    }

    // DELETE: Delete a promotion by ID
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePromotion(@PathVariable("id") Long id) {
        if (promotionService.deletePromotion(id)) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }

    // 同步所有活跃促销到生鲜商品
    @PostMapping("/actions/sync-all")
    public ResponseEntity<String> syncAllActivePromotions() {
        try {
            promotionService.syncPromotionPrices();
            return ResponseEntity.ok("所有活跃促销同步成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("同步失败: " + e.getMessage());
        }
    }


}
